package com.cip.master.action;

import java.util.List;
import java.util.Locale;

import com.cip.admin.model.User;
import com.cip.common.util.SecurityUtil;
import com.cip.master.model.Academic;
import com.cip.master.service.AcademicService;
import com.cip.webapp.action.BaseAction;
import java.text.SimpleDateFormat;

public class AcademicAction extends BaseAction{
	
	private AcademicService  academicService;
	private Academic academic;
	private List<Academic> academicList;
	
	public String saveOrUpdateAcademic() throws Exception {
		User user = SecurityUtil.getThreadLocalUser().get();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String fromYear = sdf.format(academic.getFromDate());
		String toYear = sdf.format(academic.getToDate());
		academic.setAcademicYear(fromYear+"-"+toYear);
		academic.setTenantId(user.getTenantId());
		academicService.saveOrUpdateAcademic(academic);
		saveMessage(getText("academic.added"));		
		return SUCCESS;
	}

	public String showAcademicList() throws Exception {
		
		Locale locale = getRequest().getLocale();
		String academicString = academicService.getAcademicGridList(locale);
				
		getRequest().setAttribute("academicgGridString", academicString);
		
		//setAcademicList(academicService.getAllAcademicList());
		return SUCCESS;
	}

	public String showEditAcademic() {
		try {
			setAcademic(academicService.getAcademicById(academic.getId()));
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("Error while Edit Academic: "+e.getMessage());
		}
		return SUCCESS;
	}
	
	public String showAcademic() throws Exception {
		setAcademic(new Academic());
		return SUCCESS;
	}

	public AcademicService getAcademicService() {
		return academicService;
	}

	public void setAcademicService(AcademicService academicService) {
		this.academicService = academicService;
	}

	public Academic getAcademic() {
		return academic;
	}

	public void setAcademic(Academic academic) {
		this.academic = academic;
	}

	public List<Academic> getAcademicList() {
		return academicList;
	}

	public void setAcademicList(List<Academic> academicList) {
		this.academicList = academicList;
	}
}
